package com.pmy.controller;

import com.pmy.pojo.Role;
import com.pmy.pojo.User;
import com.pmy.service.RoleServiceImpl;
import com.pmy.service.UserServiceImpl;
import com.pmy.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    RoleServiceImpl roleService;
    //条件查询
    @RequestMapping(value = "/userList")
    public String userList(Model model, HttpServletRequest request){
        //获取搜索框的值
        String queryUserName = request.getParameter("queryName");
        String temp = request.getParameter("queryUserRole");
        String pageIndex = request.getParameter("pageIndex");//当前页
        String queryUserRole = null;
        List<User> userList = null;//用户数据
        //设置页面容量
        int pageSize = 10;//写死页面容量
        //当前页
        int currentPageNo = 1;//第一次当前页写死为第一页
        if (temp == ""){
            queryUserRole = null;//如果为空字符串设置为Null
        }else {
            queryUserRole = temp;//如果不为空设置为前端传过来的queryUserRole的临时值
        }
        if (queryUserName == ""){
            queryUserName = null;
        }


        if (pageIndex != null){
            try {
                currentPageNo = Integer.parseInt(pageIndex);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }


        List<Role> roleList;//角色列表
        int totalCount = 0;//用户数据条数

        //获取用户列表
        roleList = roleService.getRoleList();
        //获取用户总数
        totalCount = userService.getUserCount(queryUserRole, queryUserName);
        //总页数支持
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        //控制首页和尾页、如果页面小于1了，就显示第一页
        int totalPageCount = pageSupport.getTotalPageCount();
        if (totalPageCount < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }
        

        //获取用户数据
        userList = userService.queryAllUser(queryUserRole,queryUserName,(currentPageNo-1)*pageSize,pageSupport.getPageSize());

        //前端需要的值
        model.addAttribute("queryName",queryUserName);
        model.addAttribute("queryUserRole",temp);
        //角色列表
        model.addAttribute("roleList",roleList);
        //查询的用户数据
        model.addAttribute("userList",userList);

        //分页支持
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);

        return "userlist";
    }





    //到添加页
    @RequestMapping(value = "/reachUser")
    public String reachUser(String userId,Model model){
//        为空返回添加页面
        if (Integer.parseInt(userId) == 0){
            return "adduser";
        }
//          userId有值返回到修改页面
        User user = null;
        try {
            user = userService.getUserById(Integer.parseInt(userId));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        model.addAttribute("user",user);
        return "updateuser";
    }
    //向数据库添加数据
    @RequestMapping(value = "/addUser")
    public String addUser(HttpServletRequest request){
        String userCode = request.getParameter("userCode");
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String userRole = request.getParameter("userRole");

        User user = null;
        try {
            user = new User();
            user.setUserCode(userCode);
            user.setUserName(userName);
            user.setUserPassword(userPassword);
            user.setGender(Integer.parseInt(gender));
            user.setBirthday(birthday);
            user.setPhone(phone);
            user.setAddress(address);
            user.setUserRole(Long.parseLong(userRole));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        int adduser = 0;
        try {
            adduser = userService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/userList";
        }



        if (adduser == 1){
            request.getSession().setAttribute("msg1","添加成功");
        }else {
            request.getSession().setAttribute("msg0","添加失败");
        }

        //添加基本功能实现

        return "redirect:/userList";
    }



    //删除一条的实现
    @RequestMapping(value = "/deleteUser")
    public String deleteUseer(String userId,HttpServletRequest request){

        int deleteUser = 0;
        try{
            deleteUser = userService.deleteUserById(Integer.parseInt(userId));
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (deleteUser == 1){
            request.getSession().setAttribute("msg1","删除成功");
        }else {
            request.getSession().setAttribute("msg0","删除失败");
        }

        return "redirect:/userList";
    }


    @RequestMapping(value = "/updateUser")
    public String updateUser(String userId,HttpServletRequest request){
        String userCode = request.getParameter("userCode");
        String userName = request.getParameter("userName");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String userRole = request.getParameter("userRole");

        User user = null;
        try {
            user = new User();
            user.setId(Integer.parseInt(userId));
            user.setUserCode(userCode);
            user.setUserName(userName);
            user.setGender(Integer.parseInt(gender));
            user.setBirthday(birthday);
            user.setPhone(phone);
            user.setAddress(address);
            user.setUserRole(Long.parseLong(userRole));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return "redirect:/userList";//捕获异常结束修改
        }
        System.out.println(user);

        int updateUserById = 0;
        try {
            updateUserById = userService.updateUserById(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (updateUserById == 1){
            request.getSession().setAttribute("msg1","修改成功");
        }else {
            request.getSession().setAttribute("msg0","修改失败");
        }

        return "redirect:/userList";
    }



    @RequestMapping(value = "changepassword")
    public String ChangePassword(){return "changepassword";}

}
